package org.gitman.agent.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

import org.gitman.agent.platform.Agent;
import org.gitman.agent.platform.PlatformManager;
import org.gitman.agent.tools.ResourcesLoader;

public class SimulationPanel extends JPanel {

	private PlatformManager platformManager;
	private JLabel iterationLabel, stepLabel;
	private AgentTableModel agentTableModel;
	private JTable table;
	private AddAgentDialog addAgentDialog;
	
	public SimulationPanel(final PlatformManager platformManager, final MainFrame mainFrame) {
		this.platformManager = platformManager;
		
		setLayout(new BorderLayout());
		
		JPanel controlPanel = new JPanel(new BorderLayout());
		JPanel iterStepPanel = new JPanel(new GridLayout(2, 4));
		iterStepPanel.add(new JLabel("Iteration:"));
		iterationLabel = new JLabel("-");
		iterStepPanel.add(iterationLabel);
		iterStepPanel.add(new JLabel("Step:"));
		stepLabel = new JLabel("-");
		iterStepPanel.add(stepLabel);
		iterStepPanel.add(new JLabel());
		iterStepPanel.add(new JLabel());
		iterStepPanel.add(new JLabel());
		iterStepPanel.add(new JLabel());
		controlPanel.add(iterStepPanel, BorderLayout.NORTH);
		JPanel iconPanel = new JPanel(new GridLayout(1,6));
		JButton fbackButton = new JButton(ResourcesLoader.getIcon("fback.png"));
		fbackButton.setToolTipText("Return to the begin of the simulation");
		iconPanel.add(fbackButton);
		JButton stepButton = new JButton(ResourcesLoader.getIcon("step.png"));
		stepButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				platformManager.getSimulation().doStep();
			}
		});
		stepButton.setToolTipText("Next step of the simluation");
		iconPanel.add(stepButton);
		JButton playButton = new JButton(ResourcesLoader.getIcon("play.png"));
		playButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				platformManager.getSimulation().play();
			}
		});
		playButton.setToolTipText("Start simulation");
		iconPanel.add(playButton);
		JButton pauseButton = new JButton(ResourcesLoader.getIcon("pause.png"));
		pauseButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				platformManager.getSimulation().pause();
			}
		});
		pauseButton.setToolTipText("Pause simulation");
		iconPanel.add(pauseButton);
		JButton stopButton = new JButton(ResourcesLoader.getIcon("stop.png"));
		stopButton.setToolTipText("Stop simulation");
		iconPanel.add(stopButton);
		JButton fforwardButton = new JButton(ResourcesLoader.getIcon("fforward.png"));
		fforwardButton.setToolTipText("Go to the end of simulation (without showing steps)");
		iconPanel.add(fforwardButton);
		controlPanel.add(iconPanel, BorderLayout.CENTER);
		JPanel paramsPanel = new JPanel(new BorderLayout());
		paramsPanel.setBorder(BorderFactory.createTitledBorder("Simmulation additional paramters"));
		JPanel maxIter = new JPanel(new FlowLayout());
		maxIter.add(new JLabel("Maximum number of iterations"));
		final JTextField maxItField = new JTextField(6);
		maxItField.setText("100");
		maxIter.add(maxItField);
		JButton apply = new JButton("Apply");
		apply.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				platformManager.getSimulation().setMaxNumberOfIterations(Integer.parseInt(maxItField.getText()));				
			}
		});
		maxIter.add(apply);
		paramsPanel.add(maxIter, BorderLayout.NORTH);
		JPanel steptimePanel = new JPanel(new FlowLayout());
		steptimePanel.add(new JLabel("Dely between steps [ms]:       "));
		final JTextField stepTimeField = new JTextField(6);
		stepTimeField.setText("300");
		steptimePanel.add(stepTimeField);
		JButton applyTime = new JButton("Apply");
		applyTime.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				platformManager.getSimulation().setDely(Integer.parseInt(stepTimeField.getText()));				
			}
		});
		steptimePanel.add(applyTime);
		paramsPanel.add(steptimePanel, BorderLayout.SOUTH);
		controlPanel.add(paramsPanel, BorderLayout.SOUTH);
		add(controlPanel, BorderLayout.NORTH);
		
		JPanel agentPanel = new JPanel(new BorderLayout());
		agentPanel.setBorder(BorderFactory.createTitledBorder("Agents"));
		JPanel optionAgentPanel = new JPanel(new GridLayout(1,3));
		JButton add = new JButton("Add");
		addAgentDialog = new AddAgentDialog(platformManager, this, mainFrame);
		add.addActionListener(addAgentDialog);
		optionAgentPanel.add(add);
		JButton remove = new JButton("Remove");
		remove.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int [] tab = table.getSelectedRows();
				agentTableModel.removeAgents(tab);
				agentTableModel.fireTableDataChanged();
			}
		});
		optionAgentPanel.add(remove);
		JButton edit = new JButton("Edit");
		edit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedRow();
				if(row == -1){
					JOptionPane.showMessageDialog(mainFrame, "First select agent", "Error", JOptionPane.ERROR_MESSAGE);
				}else{
					Agent agent = agentTableModel.getAgentAt(row);
					new EditAgentDialog(platformManager, agent, SimulationPanel.this);
					agentTableModel.fireTableDataChanged();
				}
			}
		});
		optionAgentPanel.add(edit);
		agentPanel.add(optionAgentPanel, BorderLayout.NORTH);
		agentTableModel = new AgentTableModel(platformManager);
		table = new JTable(agentTableModel);
		JScrollPane scrollPane = new JScrollPane(table);
		table.getColumnModel().getColumn(0).setPreferredWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(300);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(100);
		table.getColumnModel().getColumn(4).setPreferredWidth(100);
		ColorRenderer colorRenderer = new ColorRenderer();
		table.setDefaultRenderer(Color.class, colorRenderer);
		agentPanel.add(scrollPane);
		add(agentPanel, BorderLayout.CENTER);

	}
	
	public AddAgentDialog getAddAgentDialog(){
		return addAgentDialog;
	}

	public void updateAgentsTab() {
		agentTableModel.fireTableDataChanged();
	}

	public void setStep(int step){
		stepLabel.setText(step + "");
	}
	
	public void setIteration(int it){
		iterationLabel.setText(it + "");
	}
}
